<template>
	<view class="main">
		<view class="mains">
			<u-sticky>
				<view class="top_c">
					<view class="top">
						<view class="items">
							<view class="lefts">
								<text>代理</text>
							</view>
							<view class="rights">
								<text>{{agentData.name}}</text>
								<text class="phone">{{agentData.phone}}</text>
							</view>
						</view>
					</view>
				</view>
			</u-sticky>
			<view class="content">
				<view v-for="(items, indexs) in agentPolicy" :key="indexs" v-if="items.deposit_type == 1" class="types">
					<view class="lists standard">
						<view class="title">
							<view class="lefts">
								<text>{{items.product}}</text>
							</view>
							<view class="rights">
								<view class="btns" @click="setAll(items)">
									<text>应用所有产品</text>
								</view>
							</view>
						</view>
						<view class="list-item">
							<view class="tops">
								<view class="top-item-times">
									<text>时间段</text>
									<text class="units">(天)</text>
								</view>
								<view class="top-item-credit">
									<text>贷记卡</text>
									<text class="units">(‱)</text>
								</view>
								<view class="top-item-scan">
									<text>扫码</text>
									<text class="units">(‱)</text>
								</view>
							</view>
							<view v-for="(item, index) in items.trades" :key="index" class="setting" :class="[index==0?'border_bottom':'']">
								<view class="item-times">
									<text>{{item.start_day+1}}</text>
									<text>~</text>
									<text>{{item.end_day < 3650 ? item.end_day : '长期'}}</text>
								</view>
								<view class="item-credit">
									<input type="number" :placeholder="item.min_money + ' ~ ' + item.max_money"
										v-model="item.money">
								</view>
								<view class="item-scan">
									<input type="number" :placeholder="item.scan_min_money + ' ~ ' + item.scan_max_money"
										v-model="item.scan_money">
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="placeholders"></view>
			<view class="saveBtn" @click="saveData">
				<text>提交</text>
			</view>
		</view>
	</view>
</template>

<script>
	import tabbarItem from '../../../uni_modules/uview-ui/libs/config/props/tabbarItem'
	var util = require('@/util/request.js');

	export default {
		data() {
			return {
				agent_id: 0,
				agentData: {},
				agentPolicy: null,
				settings: 1,
				is_true: true,
				title: '',
				data: [],
				policyMsg: '确定要提交吗？',
			}
		},
		onLoad(e) {
			this.agent_id = e.id
			this.getAgent()
			this.loadData()
		},
		methods: {
			// 获取代理
			async getAgent() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'user/getAgent',
					data: {
						agent_id: this.agent_id
					}
				});
				if (res.code == 200) {
					this.agentData = res.data
				}
			},
			// 获取数据
			async loadData() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getAgentHzyPolicyNew',
					data: {
						agent_id: this.agent_id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.data = res.data
					if (res.data.policy && res.data.policy != null) {
						this.agentPolicy = res.data.policy
					}
					this.settings = res.data.settings
				}
			},
			// 押金选择
			depositTypeChange(index) {
				if (this.deposit_type != index) {
					this.deposit_type = index
					this.deposit_types = index
					this.loadData()
				}
			},
			// 保存数据
			saveData() {
				var _this = this
				var is_verify = true
				// 交易分润 - 验证
				for (var i in this.agentPolicy) {
					var msg = ""
					var is_trues = true
					if (this.agentPolicy[i].deposit_type == 1) {
						for (var j in this.agentPolicy[i].trades) {
							if (this.agentPolicy[i].trades[j].money == '' || this.agentPolicy[i].trades[j].money == null) {
								msg = '请设置【' + this.agentPolicy[i].product + '】【' + this.agentPolicy[i].trades[j]
									.start_day + '~' + (this.agentPolicy[i].trades[j].end_day < 3650 ? this.agentPolicy[i]
										.trades[j].end_day : '长期') + '】 - 贷记卡结算费率！';
								is_trues = false
								break
							}
							if (this.agentPolicy[i].trades[j].money * 100 < this.agentPolicy[i].trades[j].min_money *
								100 || this.agentPolicy[i].trades[j].money * 100 > this.agentPolicy[i].trades[j]
								.max_money * 100) {
								msg = '【' + this.agentPolicy[i].product + '】【' + this.agentPolicy[i].trades[j].start_day +
									'~' + (this.agentPolicy[i].trades[j].end_day < 3650 ? this.agentPolicy[i].trades[j]
										.end_day : '长期') + '】 - 贷记卡结算费率超出限制！';
								is_trues = false
								break
							}
							if (this.agentPolicy[i].trades[j].scan_money == '' || this.agentPolicy[i].trades[j]
								.scan_money == null) {
								msg = '请设置【' + this.agentPolicy[i].product + '】【' + this.agentPolicy[i].trades[j]
									.start_day + '~' + (this.agentPolicy[i].trades[j].end_day < 3650 ? this.agentPolicy[i]
										.trades[j].end_day : '长期') + '】 - 扫码结算费率！';
								is_trues = false
								break
							}
							if (this.agentPolicy[i].trades[j].scan_money * 100 < this.agentPolicy[i].trades[j]
								.min_scan_money * 100 || this.agentPolicy[i].trades[j].scan_money * 100 > this.agentPolicy[
									i].trades[j].max_scan_money * 100) {
								msg = '【' + this.agentPolicy[i].product + '】【' + this.agentPolicy[i].trades[j].start_day +
									'~' + (this.agentPolicy[i].trades[j].end_day < 3650 ? this.agentPolicy[i].trades[j]
										.end_day : '长期') + '】 - 扫码结算费率超出限制！';
								is_trues = false
								break
							}
						}
						if (!is_trues) {
							is_verify = false
							uni.showToast({
								title: msg,
								icon: 'none',
								duration: 1000
							})
							return false
						}
					}
				}
				if (is_verify) {
					for (var i in this.agentPolicy) {
						for (var j in this.agentPolicy[i].trades) {
							if (this.agentPolicy[i].trades[j].money == '' || this.agentPolicy[i].trades[j].money == null) {
								for (var k in this.agentPolicy) {
									if (this.agentPolicy[k].product_id == this.agentPolicy[i].product_id && this
										.agentPolicy[k].deposit_type == 1) {
										for (var s in this.agentPolicy[k].trades) {
											if (this.agentPolicy[k].trades[s].start_day == this.agentPolicy[i].trades[j]
												.start_day) {
												this.agentPolicy[i].trades[j].money = this.agentPolicy[k].trades[s].money
												this.agentPolicy[i].trades[j].scan_money = this.agentPolicy[k].trades[s]
													.scan_money
											}
										}
									}
								}
							}
						}
					}
					uni.showModal({
						content: this.policyMsg,
						success: (res) => {
							if (res.confirm) {
								_this.saveDataFun()
							}
						}
					})
				}
			},
			// 保存数据 - 方法
			async saveDataFun() {
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp({
						url: 'v1/saveAgentHzyPolicyNew',
						data: {
							policy: this.agentPolicy,
							settings: this.settings,
							agent_id: this.agent_id,
						}
					});
					uni.hideLoading()
					this.is_true = true
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
					if (res.code == 200) {
						setTimeout(function() {
							uni.navigateBack({});
						}, 1000)
					}
				}
			},
			// 应用所有产品
			setAll(item) {
				this.agentPolicy.forEach((itemA, indexA) => {
					itemA.trades.forEach((itemB, indexB) => {
						item.trades.forEach((itemC, indexC) => {
							if (itemB.start_day == itemC.start_day && itemB.end_day == itemC
								.end_day) {
								itemB.money = itemC.money
								itemB.scan_money = itemC.scan_money
							}
						})
					})
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F6FC;
	}

	input {
		font-size: 28rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		// width: 200rpx;
		background-color: #DCDFE6;
	}

	.main {
		.mains {

			.top_c {
				padding: 30rpx;
				background-color: #F2F6FC;

				.top {
					padding: 0 30rpx;
					background-color: #ffffff;
					border-radius: 18rpx;
					box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

					.items {
						padding: 30rpx 0;
						display: flex;
						justify-content: space-between;
						border-bottom: 1px solid #EBEEF5;

						.lefts {
							font-weight: bold;
						}

						.rights {
							display: flex;
							align-items: center;
							color: #82848a;

							.phone {
								padding-left: 16rpx;
							}

							.products {
								padding-right: 16rpx;
							}
						}
					}
				}

				.tags {
					margin-top: 30rpx;
				}
			}

			.content {
				padding: 0 30rpx;

				.types {

					.lists {
						padding: 30rpx 30rpx 0 30rpx;
						background-color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
						margin-bottom: 30rpx;

						.title {
							text-align: center;
							padding-bottom: 30rpx;
							border-bottom: 1px solid #EBEEF5;
							font-weight: bold;
							display: flex;
							justify-content: space-between;
							align-items: center;

							.rights {
								display: flex;
								align-items: center;

								.btns {
									padding: 10rpx 20rpx;
									border-radius: 8rpx;
									border: 1px solid #75B6F8;
									font-size: 24rpx;
									color: #75B6F8;
								}
							}
						}

						.items {
							display: flex;
							justify-content: space-between;
							align-items: center;
							padding: 20rpx 0;
							border-top: 1px solid #EBEEF5;

							.radios {
								width: 100%;
								padding-top: 10rpx;
								padding-bottom: 20rpx;
							}

							.rights {
								display: flex;
								justify-content: flex-end;
								align-items: center;

								input {
									width: 70%;
									text-align: center;
									padding-right: 60rpx;
								}

								text {
									position: absolute;
									padding-right: 10rpx;
								}

								.flow_unit {
									padding-right: 80rpx;
								}
							}
						}

						.items-add {
							border-top: 1px solid #EBEEF5;
							padding: 20rpx 0rpx;
							color: #2979ff;

							.add-btn {
								display: flex;
								justify-content: center;
								align-items: center;
								text-align: center;

								text {
									padding-left: 10rpx;
								}
							}
						}

						.list-item {
							margin-top: 30rpx;
							border-radius: 10rpx;
							border: 1px dashed #c8c9cc;
							padding: 20rpx;

							.tops {
								display: flex;
								font-size: 28rpx;
								font-weight: bold;
								border-bottom: 1px solid #EBEEF5;
								padding-bottom: 20rpx;

								.top-item-times {
									width: 40%;
								}

								.top-item-credit {
									width: 30%;
									text-align: center;
								}

								.top-item-scan {
									text-align: center;
									width: 30%;
								}

								.units {
									font-size: 24rpx;
									color: #c8c9cc;
								}
							}

							.setting {
								display: flex;
								align-items: center;
								padding-top: 20rpx;

								.item-times {
									width: 40%;
									font-size: 28rpx;
								}

								.item-credit {
									width: 30%;
									padding: 0 20rpx;
								}

								.item-scan {
									width: 30%;
								}

								input {
									text-align: center;
								}
							}

							.border_bottom {
								padding-bottom: 20rpx;
								border-bottom: 1px dashed #EBEEF5;
							}
						}
					}

					.standard {
						padding-bottom: 30rpx;
					}

					.lists-add {
						padding: 30rpx 0rpx;
						background-color: #2979ff;
						color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

						.add-btn {
							display: flex;
							justify-content: center;
							align-items: center;
							text-align: center;

							text {
								padding-left: 10rpx;
							}
						}
					}
				}
			}

			.placeholders {
				height: 90rpx;
			}

			.saveBtn {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				text-align: center;
				background-color: #409EFF;
				color: #ffffff;
				padding: 20rpx 0;
			}
		}
	}
</style>